package com.turing.post.salesmanage.impl;

import javax.ejb.EJB;
import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;

import com.turing.post.common.util.SysOperLog;
import com.turing.post.common.util.SysOperLogDao;
import com.turing.post.salesmanage.util.OrganizeInfDetail;
import com.turing.post.salesmanage.util.OrganizeInfDetailBusiness;
import com.turing.post.salesmanage.util.OrganizeInfDetailDao;
import com.turing.post.salesmanage.util.SalesManageApplicationException;
import com.turing.post.salesmanage.util.SalesManageRespCode;
import com.turing.post.util.PCPSApplicationException;
import com.turing.post.util.PCPSUtil;
import com.turing.post.util.PCPSWebInput;

/**
 * @author zxp
 * @Dec 22, 2012
 */
@Stateless(mappedName = "salesmanage.impl.OrganizeInfDetailBusinessImpl")
@Remote
@Local
public class OrganizeInfDetailBusinessImpl implements OrganizeInfDetailBusiness {

	@EJB(mappedName = "common.impl.SysOperLogDaoImpl")
	private SysOperLogDao sysOperLogDaoImpl;

	@EJB(mappedName = "salesmanage.impl.OrganizeInfDetailDaoImpl")
	private OrganizeInfDetailDao organizeInfDetailDaoImpl;

	@Override
	public OrganizeInfDetail insertOrganizeInfDetailInfo(PCPSWebInput webInput,
			OrganizeInfDetail organizeInfDetail)
			throws PCPSApplicationException {
		// 对象
		if (organizeInfDetail == null) {
			throw new SalesManageApplicationException(
					SalesManageApplicationException.ERRCODE_CATEGORY_APPLICATION,
					SalesManageRespCode.ORGANIZECUSTOM_ARGS_NULL,
					"单位证件明细输入为NULL");
		}

		// 单位ID
		if (organizeInfDetail.getOrganizeId() == null) {
			throw new SalesManageApplicationException(
					SalesManageApplicationException.ERRCODE_CATEGORY_APPLICATION,
					SalesManageRespCode.ORGANIZECUSTOM_ARGS_NULL, "单位证件ID为NULL");
		}

		// 获取序列号
		organizeInfDetail.setSeqNo(organizeInfDetailDaoImpl
				.getMaxSeqNoByOrganizeId(organizeInfDetail.getOrganizeId()));

		// 证件类型，证件号
		if (organizeInfDetail.getIdTypeId() == null
				|| PCPSUtil.isEmpty(organizeInfDetail.getIdNo())) {
			throw new SalesManageApplicationException(
					SalesManageApplicationException.ERRCODE_CATEGORY_APPLICATION,
					SalesManageRespCode.ORGANIZECUSTOM_ARGS_NULL,
					"证件类型或证件号为NULL");
		}
		// 日志记录
		sysOperLogDaoImpl.insertSysOperLogInfo(webInput,
				SysOperLog.operType_Add, "创建单位证件信息", "创建单位证件信息，单位信息证件号 "
						+ organizeInfDetail.getIdNo());
		// 插入单位证件明细信息
		organizeInfDetailDaoImpl.insertOrganizeInfDetail(organizeInfDetail);
		return organizeInfDetail;
	}

	@Override
	public void updateOrganizeInfDetailInfo(PCPSWebInput webInput,
			OrganizeInfDetail organizeInfDetail)
			throws PCPSApplicationException {
		OrganizeInfDetail _organizeInfDetail = organizeInfDetailDaoImpl
				.getOrganizeInfDetailByOrganizeInfDetail(organizeInfDetail);
		// 对象
		if (_organizeInfDetail == null) {
			throw new SalesManageApplicationException(
					SalesManageApplicationException.ERRCODE_CATEGORY_APPLICATION,
					SalesManageRespCode.ORGANIZECUSTOM_ARGS_NULL,
					"待修改单位证件明细信息数据库中不存在!");
		}
		// 对象赋值
		_organizeInfDetail.setIdTypeId(organizeInfDetail.getIdTypeId());
		_organizeInfDetail.setIdNo(organizeInfDetail.getIdNo());
		_organizeInfDetail.setRemark(organizeInfDetail.getRemark());
		_organizeInfDetail.setIdImageId(organizeInfDetail.getIdImageId());
		// 日志记录
		sysOperLogDaoImpl.insertSysOperLogInfo(webInput,
				SysOperLog.operType_Add, "修改单位证件信息", "修改单位证件信息，单位信息证件号 "
						+ organizeInfDetail.getIdNo());
		// 插入单位证件明细信息
		organizeInfDetailDaoImpl.updateOrganizeInfDetail(_organizeInfDetail);
	}
}
